package com.dhcc.cdc.sys.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.entity.KettleJobLogEntity;
import com.dhcc.cdc.sys.service.IKettleJobLogService;
import com.dhcc.cdc.util.KettleUtil;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * @author sunyj
 * @ClassName: KettleJobLogController
 * @Description: TODO
 * @date 2020-06-24 10:15:54
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/kettlejoblog")
public class KettleJobLogController extends BaseController {

    private String PREFIX = "/backend/cdc/kettlejoblog/";

    @Autowired
    private IKettleJobLogService kettleJobLogService;


    /**
     * 跳转到列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "kettlejoblog.html";
    }

    /**
     * 获取列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        Page<KettleJobLogEntity> page = new PageFactory<KettleJobLogEntity>().defaultPage();
        List<KettleJobLogEntity> result = this.kettleJobLogService.page(page, params);
        page.setRecords((List<KettleJobLogEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * @param params
     * @Description: 查询日志详细信息
     * @Title: searchLogFieId
     * @return: Object
     * @author: wujiafeng
     * @date:2020年6月24日 上午10:50:55
     */
    @RequestMapping(value = "/searchLogFieId")
    @ResponseBody
    public Object searchLogFieId(@RequestParam Map<String, Object> params) {
        Wrapper<KettleJobLogEntity> wrapper = new EntityWrapper<KettleJobLogEntity>();
        wrapper.eq("ID_JOB", params.get("idJob"));
        KettleJobLogEntity kettle = kettleJobLogService.selectOne(wrapper);
        return kettle.getLogField();
    }

    @RequestMapping(value = "/synData")
    @ResponseBody
    public Object synData(@RequestParam Map<String, Object> params) {
        return kettleJobLogService.syndata(params);
    }

    @RequestMapping(value = "/getErrorJobLog")
    @ResponseBody
    public Object getErrorJobLog(@RequestParam Map<String, Object> params) {
        Wrapper<KettleJobLogEntity> wrapper = new EntityWrapper<KettleJobLogEntity>();
        wrapper.ne("ERRORS", 0);
        wrapper.eq("is_read", 0);
        wrapper.between("LOGDATE", DateUtil.getAfterDayDate("-7"), DateUtil.getTime());
        wrapper.orderBy("LOGDATE", false);
        List<KettleJobLogEntity> result = this.kettleJobLogService.selectList(wrapper);
        return result;
    }

    @RequestMapping(value = "/updateLogReadStatus")
    @ResponseBody
    public Object updateLogReadStatus(@RequestParam Map<String, Object> params) {
        if (CommonUtil.isEmpty(params.get("logIds"))) {
            return false;
        }
        JSONArray logIds = JSONArray.parseArray(String.valueOf(params.get("logIds")));
        return this.kettleJobLogService.updateForSet("is_read=1", new EntityWrapper<KettleJobLogEntity>().in("ID_JOB", logIds));
    }

    @RequestMapping(value = "/getLogs")
    @ResponseBody
    public Object getLogs() {
        return KettleUtil.getLogs();
    }
}
